package com.sfdk.palettes
{
	public class Palette
	{
		private var redArray_ : Array;
		private var greenArray_ : Array;
		private var blueArray_ : Array;
		private var name_ : String;
				
		public function Palette(data : XML = null)
		{
			redArray_ = new Array(256);
			greenArray_ = new Array(256);
			blueArray_ = new Array(256);	
			
			if(data != null)
			{
				name_ = data.name;
				var offsets : XMLList = data.elements("offset");
				var offsetLength : int = offsets.length(), i : int;
				var currentOffset : XML;
				var currentOffsetId : int;
				var redValue : int, greenValue : int, blueValue : int;
				for(i = 0; i < offsetLength; i++)
				{
					currentOffset = offsets[i];
					currentOffsetId = currentOffset.id;
					
					redValue = currentOffset.redValue;
					greenValue = currentOffset.greenValue;
					blueValue = currentOffset.blueValue;
					
					redArray_[currentOffsetId] = redValue;
					greenArray_[currentOffsetId] = greenValue;
					blueArray_[currentOffsetId] = blueValue
				}
			}
		}
		
		public function get red() : Array
		{
			return redArray_;
		}
		
		public function set red(value : Array) : void
		{
			redArray_ = value;
		}
		
		public function get green() : Array
		{
			return greenArray_;
		} 
		
		public function set green(value : Array) : void
		{
			greenArray_ = value;
		} 
		
		public function get blue() : Array
		{
			return blueArray_;
		}
		
		public function set blue(value : Array) : void
		{
			blueArray_ = value;
		}
		
		public function add(addedPalette : Palette) : Palette
		{
			var newPalette : Palette = new Palette();
			var newRed : Array = new Array();
			var addedRed : Array = addedPalette.red;
			var key : String;
			for (key in addedRed)
			{
				newRed[key]=addedRed[key];
			}
			for (key in redArray_)
			{
				newRed[key]=redArray_[key];
			}
			newPalette.red = newRed;
			
			var newGreen : Array = new Array();
			var addedGreen : Array = addedPalette.green;
			for (key in addedGreen)
			{
				newGreen[key]=addedGreen[key];
			}
			for (key in greenArray_)
			{
				newGreen[key]=greenArray_[key];
			}
			newPalette.green = newGreen;
			
			var newBlue : Array = new Array();
			var addedBlue : Array = addedPalette.blue;
			for (key in addedBlue)
			{
				newBlue[key]=addedBlue[key];
			}
			for (key in blueArray_)
			{
				newBlue[key]=blueArray_[key];
			}
			newPalette.blue = newBlue;
			
			return newPalette;
		}

	}
}